package HCI.src;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Browse extends Activity {
	OnClickListener lunchListner;
	OnClickListener viewDesc;
	OnTouchListener touchListener;
	OnFocusChangeListener focusListener;
    /** Called when the activity is first created. */
	private void setCategories()
	{
		String qry= "SELECT cat_name FROM category";
		//Connector con =new Connector(qry);
		String [][]cats ={new String[]{"Breakfast"},new String[]{"Lunch"}};
		TextView [] categories = new TextView[cats.length];
		final int CAT_WIDTH=50;
		final int CAT_HEIGHT=20;
		int currY=0;
		for(int i=0;i<cats.length;i++)
		{
			CharSequence cat_Name = cats[i][0];
			categories[i]=new TextView(this);
			categories[i].setText(cat_Name);
			categories[i].setTextAppearance(this, R.style.browseItemText);
			View bLayout=findViewById(R.id.browsingLayout);
			((LinearLayout)bLayout).addView(categories[i]);
		}
		
		
	}
	private void initializeComponents()
	{
		//setCategories();
		lunchListner=new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView rView=(TextView)findViewById(R.id.rLabel);
				rView.setVisibility((rView.getVisibility()==View.GONE)?View.VISIBLE:View.GONE);
			}
		};
		viewDesc = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent dest=new Intent(v.getContext(),Description.class);
				startActivity(dest);
			}
		};
		touchListener = new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				TextView textView = (TextView) findViewById(v.getId());
				SpannableString content = new SpannableString(textView.getText());
				content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
				textView.setText(content);
				return true;
			}
		};
		focusListener=new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				//Toast t= new Toast(v.getContext());
				//UnderlineSpan ulsp=new UnderlineSpan();
				if(hasFocus)
				{
					TextView textView = (TextView) findViewById(v.getId());
					SpannableString content = new SpannableString(textView.getText());
					content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
					textView.setText(content);
					CharSequence mesage = textView.getText();
					//t.setText(mesage);
				}
				else
				{
					TextView textView = (TextView) findViewById(v.getId());
					SpannableString content = new SpannableString(textView.getText());
					content.removeSpan(content.getSpans(0, content.length(),UnderlineSpan.class)[0]);
					//content.setSpan(new StyleSpan(Typeface.ITALIC), 0, content.length(), 0);
					textView.setText(content);
					CharSequence mesage = textView.getText();
					//t.setText(mesage);
				}
				//t.show();
			}
		};
        
        
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeComponents();
        setContentView(R.layout.browse);
        TextView lunchLabel=(TextView)findViewById(R.id.lLabel);
		lunchLabel.setOnClickListener(lunchListner);
		LinearLayout main=(LinearLayout)findViewById(R.id.browsingLayout);
		int count = main.getChildCount();
		for(int i=0;i<count;i++)
		{
			View child=main.getChildAt(i);
			child.setOnFocusChangeListener(focusListener);
		}
		((TextView)findViewById(R.id.rLabel)).setOnClickListener(viewDesc);

}
}